package org.yuangu.privacyfinance.activities.viewmodel;

import android.app.Application;

import org.yuangu.privacyfinance.domain.FinanceDatabase;
import org.yuangu.privacyfinance.domain.access.CategoryDao;
import org.yuangu.privacyfinance.domain.model.Category;
import org.yuangu.privacyfinance.helpers.CurrencyHelper;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.Bindable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

public class CategoryDialogViewModel extends BindableViewModel {
    private CategoryDao categoryDao = FinanceDatabase.getInstance().categoryDao();
    private long categoryId;
    private Category category;
    private LiveData<Category> categoryLive;
    private String originalName;
    private Integer originalColor;
    private Long originalBudget;

    public CategoryDialogViewModel(@NonNull Application application) {
        super(application);
    }

    public LiveData<List<Category>> getAllCategories() {
        return categoryDao.getAll();
    }

    public LiveData<Category> setCategoryId(long categoryId) {
        if (this.categoryId != categoryId) {
            this.categoryId = categoryId;
            if (categoryId == -1) {
                setCategoryDummy();
            } else {
                categoryLive = categoryDao.get(categoryId);
            }
        }
        return categoryLive;
    }

    private void setCategoryDummy() {
        MutableLiveData<Category> mutableCategory = new MutableLiveData<>();
        mutableCategory.postValue(new Category());
        categoryLive = mutableCategory;
    }

    public void setCategory(Category category) {
        this.category = category;
        originalName = category.getName();
        originalColor = category.getColor();
        originalBudget = category.getBudget();
        notifyChange();
    }

    public boolean isNewCategory() {
        return category.getId() == null;
    }

    @Bindable
    public String getName() {
        if (category == null) return null;
        return category.getName();
    }

    public void setName(String name) {
        if (name == null) name = "";
        if (category.getName() == null) category.setName("");
        if (!category.getName().equals(name)) {
            category.setName(name);
            notifyPropertyChanged(BR.name);
        }
    }

    @Bindable
    public String getBudget() {
        if (category == null) return null;
        return CurrencyHelper.convertToString(category.getBudget());
    }

    public void setBudget(String budget) {
        if (budget == null) budget = "";
        category.setBudget(CurrencyHelper.convertToLong(budget));
    }

    @Bindable
    public Integer getColor() {
        if (category == null) return null;
        return category.getColor();
    }

    public void setColor(Integer color) {
        if (category.getColor() != color) {
            category.setColor(color);
            notifyPropertyChanged(BR.color);
        }
    }

    public void submit() {
        categoryDao.updateOrInsertAsync(category);
    }

    public void cancel() {
        category.setName(originalName);
        category.setColor(originalColor);
        category.setBudget(originalBudget);
    }

    public Category getCategory() {
        return category;
    }
}
